﻿using StandardManager.DBItems;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StandardManager.EntityCode
{
    public class EntityCode
    {
        public string Id { get; set; }
        public string ParentId { get; set; } = "";
        public string CodeValue { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public EntityCode() { }
        public EntityCode(DataRow dataRow)
        {
            Id  = dataRow["Id"].ToString();
            ParentId = dataRow["ParentId"].ToString();
            CodeValue = dataRow["CodeValue"].ToString();
            Name = dataRow["Name"].ToString();
            Description = dataRow["Description"].ToString();
        }

        internal static string GetTableCreateSql()
        {
            return @"  
                CREATE TABLE IF NOT EXISTS EntityCode (  
                    Id TEXT NOT NULL,  
                    ParentId TEXT,  
                    CodeValue TEXT NOT NULL,  
                    Name TEXT NOT NULL,  
                    Description TEXT  
                );";
        }

        public override string ToString()
        {
            return CodeValue + "(" + Name + ")";
        }

        internal string BuildInsertSql()
        {
            var str = "insert OR IGNORE into EntityCode(Id,ParentId,CodeValue,Name,Description) values('{0}','{1}','{2}','{3}','{4}')";
            return String.Format(str,Id, ParentId,CodeValue, Name, Description);
        }
        internal string BuildUpdateSql()
        {
            var str = "update EntityCode set ParentId='{1}', CodeValue='{2}',Name='{3}',Description='{4}' where id='{0}'";
            return String.Format(str, Id, ParentId, CodeValue, Name, Description);
        }

        internal string BuildDeleteSql()
        {
            var str = "delete from  EntityCode  where id='{0}'";
            return String.Format(str, Id);
        }
    }

    public class EntityCodeWithTreeNode
    {
        public EntityCode Item { get; set; }
        public TreeNode TreeNode { get; set; }
    }
    public class EntityCodeSorter : IComparer<EntityCodeWithTreeNode>
    {
        public int Compare(EntityCodeWithTreeNode? x, EntityCodeWithTreeNode? y)
        {
            return x.Item.CodeValue.CompareTo(y.Item.CodeValue);
        }
    }
}
